# -*- coding: utf-8 -*-
# https://blog.csdn.net/weixin_52040868
# 公众号：测个der
# 微信：qing_an_an

import sys
import time

from PyQt5.QtWidgets import *
from PyQt5.QtCore import QThread,pyqtSignal,QDateTime

class TimeBack(QThread):
    update_data = pyqtSignal(str)

    def run(self) -> None:
        while True:
            data = QDateTime.currentDateTime()
            currTime = data.toString("yyyy-MM-dd hh:mm:ss")
            self.update_data.emit(str(currTime))
            time.sleep(1)

class QWIN(QWidget):
    def __init__(self):
        super(QWIN, self).__init__()
        self.thread = MyThread()
        self.thread.setIdentity('thread1')
        self.thread.sinOut.connect(self.outText)
        self.thread.setVal(6)

        self.edit = QLineEdit(self)
        # 类似主线程中创建子线程
        self.back = TimeBack()
        # 连接信号
        self.back.update_data.connect(self.back_)
        # 启动线程
        self.back.start()

    def back_(self,text):
        self.edit.setText(text)

    def outText(self,text):
        print(text)

class MyThread(QThread):
    sinOut = pyqtSignal(str)

    def __init__(self):
        super(MyThread, self).__init__()
        self.identity = None

    def setIdentity(self,text):
        self.identity = text

    def setVal(self,val):
        self.times = int(val)
        # 执行线程的run方法
        self.start()

    def run(self) -> None:
        while self.times > 0 and self.identity:
            self.sinOut.emit(self.identity+"---"+str(self.times))
            self.times -= 1

if __name__ == '__main__':
    app = QApplication(sys.argv)
    win = QWIN()
    win.show()
    sys.exit(app.exec_())
